package com.ssy.lingxi.transaction.controller.pc;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dataauth.annotation.common.DataAuth;
import com.ssy.lingxi.transaction.api.dto.request.*;
import com.ssy.lingxi.transaction.api.dto.response.ConfirmQuoteListResponse;
import com.ssy.lingxi.transaction.api.dto.response.NotarizeEnquiryQuotedPriceDetailsResponse;
import com.ssy.lingxi.transaction.api.dto.response.ProductQuotationDetailsResponse;
import com.ssy.lingxi.transaction.api.dto.response.QuotationExternalStateEnumResponse;
import com.ssy.lingxi.transaction.api.dto.response.*;
import com.ssy.lingxi.transaction.api.enums.ConfirmQuoteInteriorStatusEnum;
import com.ssy.lingxi.transaction.api.enums.InquiryExternalStatusEnum;
import com.ssy.lingxi.transaction.service.IConfirmQuoteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu 交易中心-确认询价报价
 * @author wrc
 * @date 2020/8/19
 */
@RestController
@RequestMapping("/transaction")
public class ConfirmQuoteController extends BaseController {

    @Resource
    IConfirmQuoteService confirmQuoteService;

    /**
     * 确认询价报价-报价单列表查询
     */
    @DataAuth(tableName = "trade_quote", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "inquiry_user_id")
    @GetMapping("/notarize/enquiry/product/quotation/list")
    public Wrapper<PageData<ConfirmQuoteListResponse>> notarizeEnquiryProductQuotationList(ProductQuotationListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryProductQuotationList(request, sysUser);
    }

    /**
     * 确认询价报价-报价单详情
     */
    @GetMapping("/notarize/enquiry/product/quotation/details")
    public Wrapper<ProductQuotationDetailsResponse> productQuotationDetails(ProductQuotationDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.productQuotationDetails(request,sysUser);
    }

    /**
     * 询价报价-待提交审核报价单列表查询
     */
    @DataAuth(tableName = "trade_quote", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "inquiry_user_id")
    @GetMapping("/notarize/enquiry/quoted/price/list")
    public Wrapper<PageData<ConfirmQuoteListResponse>> notarizeEnquiryQuotedPriceList(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceList(request,sysUser);
    }

    /**
     * 确认询价报价-待提交报价单详情
     */
    @GetMapping("/notarize/enquiry/quoted/price/details")
    public Wrapper<NotarizeEnquiryQuotedPriceDetailsResponse> notarizeEnquiryQuotedPriceDetails(NotarizeEnquiryQuotedPriceDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceDetails(request,sysUser);
    }

    /**
     * 确认询价报价-提交审核
     */
    @PostMapping("/notarize/enquiry/quoted/price/submit")
    public Wrapper notarizeEnquiryQuotedPriceSubmit(@RequestBody NotarizeEnquiryQuotedPriceSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceSubmit(request,sysUser);
    }

    /**
     * 确认询价报价-批量提交审核
     */
    @PostMapping("/notarize/enquiry/quoted/price/submit/all")
    public Wrapper notarizeEnquiryQuotedPriceSubmitAll(@RequestBody NotarizeEnquiryQuotedPriceSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceSubmitAll(request,sysUser);
    }

    /**
     * 确认询价报价-待审核报价单查询（一级）
     */
    @DataAuth(tableName = "trade_quote", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "inquiry_user_id")
    @GetMapping("/notarize/enquiry/quoted/price/audit/list")
    public Wrapper<PageData<ConfirmQuoteListResponse>> notarizeEnquiryQuotedPriceAuditList(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAuditList(request,sysUser);
    }

    /**
     * 确认询价报价-审核（一级）
     */
    @PostMapping("/notarize/enquiry/quoted/price/audit")
    public Wrapper notarizeEnquiryQuotedPriceAudit(@RequestBody NotarizeEnquiryQuotedPriceSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAudit(request,sysUser);
    }

    /**
     * 确认询价报价-批量审核（一级）
     */
    @PostMapping("/notarize/enquiry/quoted/price/audit/all")
    public Wrapper notarizeEnquiryQuotedPriceAuditAll(@RequestBody NotarizeEnquiryQuotedPriceSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAuditAll(request,sysUser);
    }

    /**
     * 确认询价报价-待审核报价单列表(二级)
     */
    @DataAuth(tableName = "trade_quote", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "inquiry_user_id")
    @GetMapping("/notarize/enquiry/quoted/price/audit/list/two")
    public Wrapper<PageData<ConfirmQuoteListResponse>> notarizeEnquiryQuotedPriceAuditListTwo(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAuditListTwo(request,sysUser);
    }

    /**
     * 确认询价报价-审核(二级)
     */
    @PostMapping("/notarize/enquiry/quoted/price/audit/two")
    public Wrapper notarizeEnquiryQuotedPriceAuditTwo(@RequestBody NotarizeEnquiryQuotedPriceSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAuditTwo(request,sysUser);
    }

    /**
     * 确认询价报价-批量审核(二级)
     */
    @PostMapping("/notarize/enquiry/quoted/price/audit/all/two")
    public Wrapper notarizeEnquiryQuotedPriceAuditAllTwo(@RequestBody NotarizeEnquiryQuotedPriceSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAuditAllTwo(request,sysUser);
    }

    /**
     * 确认询价报价-待确认报价单列表
     */
    @DataAuth(tableName = "trade_quote", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "inquiry_user_id")
    @GetMapping("/notarize/enquiry/quoted/price/affirm/list/")
    public Wrapper<PageData<ConfirmQuoteListResponse>> notarizeEnquiryQuotedPriceAffirmList(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAffirmList(request,sysUser);
    }

    /**
     * 确认询价报价-确认报价
     */
    @PostMapping("/notarize/enquiry/quoted/price/affirm")
    public Wrapper notarizeEnquiryQuotedPriceAffirm(@RequestBody NotarizeEnquiryQuotedPriceSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAffirm(request,sysUser);
    }

    /**
     * 确认询价报价-批量提交确认
     */
    @PostMapping("/notarize/enquiry/quoted/price/affirm/all")
    public Wrapper notarizeEnquiryQuotedPriceAffirmAll(@RequestBody NotarizeEnquiryQuotedPriceSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return confirmQuoteService.notarizeEnquiryQuotedPriceAffirmAll(request,sysUser);
    }

    /**
     * 确认询价报价-内部工作流程枚举
     */
    @GetMapping("/notarize/enquiry/interior/state/enum")
    private Wrapper<List<QuotationInteriorStateEnumResponse>> getNotarizeEnquiryInteriorEnum() {
        return Wrapper.success(Arrays.stream(ConfirmQuoteInteriorStatusEnum.values()).map(r -> {
            if(!ConfirmQuoteInteriorStatusEnum.NOT_REPORT_PRICE.getState().equals(r.getState())){
                QuotationInteriorStateEnumResponse status = new QuotationInteriorStateEnumResponse();
                status.setState(r.getState());
                status.setName(r.getName());
                return status;
            }else{
                return null;
            }
        }).filter(Objects::nonNull).collect(Collectors.toList()));
    }

    /**
     * 确认询价报价-外部工作流程枚举
     */
    @GetMapping("/notarize/enquiry/external/state/enum")
    private Wrapper<List<QuotationExternalStateEnumResponse>> getNotarizeEnquiryExternalEnum() {
        return Wrapper.success(Arrays.stream(InquiryExternalStatusEnum.values()).map(r -> {
            if(InquiryExternalStatusEnum.INQUIRY_SHEET_TO_BE_SUBMITTED.getState().intValue() != r.getState().intValue() && InquiryExternalStatusEnum.QUOTATION_TO_BE_SUBMITTED.getState().intValue() != r.getState().intValue()){
                QuotationExternalStateEnumResponse status = new QuotationExternalStateEnumResponse();
                status.setState(r.getState());
                status.setName(r.getName());
                return status;
            }else{
                return null;
            }
        }).filter(Objects::nonNull).collect(Collectors.toList()));
    }
}
